// 状态对象

import { creatEnum } from '@/utils/tools'
import carStatus from './car_status_conf'

export default class stateBox {

    store:stateConf[] = carStatus
    len:number = 0
    enumKeys:any
    enumLabels:any

    public constructor (status?:stateConf[]){
        
        if(status){
            this.store = status;
        }

        this.len = this.store.length;
        this.buildIndexes();
    }   

    buildIndexes (){
        let keys = {};
        let labels = {};
        
        this.store.forEach((state, index) =>{

            keys[state.label] = index;
            labels[state.key] = index;

        })
        
        this.enumKeys = creatEnum(keys);
        this.enumLabels = creatEnum(labels);

    }

    // 查找值， 未做验证
    changeKey(propName:string|number ):string|number|undefined{
        let key = this.enumKeys[propName] !== undefined ? this.enumKeys[propName] : this.enumLabels[propName];
        return key;
    }

    getVal(propName:string|number):stateConf{
        let index:number = (typeof <number>propName === 'number') ? <number>propName : <number>this.changeKey(propName);
        return this.store[index];
    }
    
}